<?php
class systemController extends RPHP_Controller
{

    //权限前缀
    protected $_pvprefix = "system";

    //新增管理员
    public function useradd()
    {

        $priv = array($this->_pvprefix . "_ua");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth  = $auth;
            $this->view->group = $model->getGroups();
            $this->display();

        }

    }

    //编辑管理员
    public function useredit()
    {

        $priv = array($this->_pvprefix . "_ue");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth  = $auth;
            $this->view->group = $model->getGroups();
            $this->view->user  = $model->getUsers($_GET['aid']);

            if ($this->view->user) {
                if ($this->view->user['last_aid'] > 0) {
                    $this->view->lastaid = $model->getAdminUsername($this->view->user['last_aid']);
                }

                $this->view->stat[$this->view->user['stat']] = " checked";

                $this->display();
            } else {
                RPHP_RR::nodata();
            }

        }

    }

    //保存管理员
    public function userdo()
    {

        $priv = array($this->_pvprefix . "_ul");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->view->code = 900;

            if ($_POST['act'] == "add" && $model->checkPriv($this->_pvprefix . "_ua", false)) {

                $this->view->success = "新增管理员成功";
                $this->view->link    = array(
                    array('name' => '继续新增管理员', 'url' => '/system/useradd'),
                    array('name' => '返回管理员列表', 'url' => '/system/user'),
                );

                $this->view->code = $model->saveUser($_POST);

            } elseif ($_POST['act'] == "edit" && $model->checkPriv($this->_pvprefix . "_ue", false)) {

                $this->view->success = "编辑管理员成功";
                $this->view->link    = array(
                    array('name' => '返回管理员列表', 'url' => '/system/user'),
                );

                $this->view->code = $model->saveUser($_POST, $_POST['aid']);

            }

            $this->displaydo();

        }

    }

    //管理员AJAX操作
    public function userajax()
    {

        $priv = array($this->_pvprefix . "_ul");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            if ($_POST['act'] == 'chkemail') {
                echo $model->checkUsername($_POST['email']);
            } elseif ($_POST['act'] == 'chkmobile') {
                echo $model->checkUsername($_POST['mobile'], 1);
            } elseif ($_POST['act'] == 'chgpriv') {
                $privs = RPHP::loadConfig("config/priv");
                echo $model->getChgpriv($_POST['groupid'], $privs);
            } elseif ($_POST['act'] == 'chgpriv_edit') {
                $privs = RPHP::loadConfig("config/priv");
                echo $model->getChgprivEdit($_POST['groupid'], $_POST['ygpid'], $_POST['aid'], $privs);
            }

        }

    }

    //管理员列表
    public function user()
    {

        $priv = array($this->_pvprefix . "_ul");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth  = $auth;
            $this->view->group = $model->getGroups();
            //获取管理员
            $users = $model->getUsers();

            if ($users['data']) {
                foreach ($users['data'] as $r) {
                    $aid[] = $r['aid'];
                    if ($r['last_aid'] > 0) {
                        $last_aid[] = $r['last_aid'];
                    }

                }

                if ($last_aid) {
                    $this->view->last_aid = $model->getAdminUsername($last_aid);
                }

                if ($aid) {
                    $this->view->online = $model->getOnline($aid);
                }

                if ($aid) {
                    $this->view->signedlog = $model->getSignedLog($aid);
                }

                //获取新增权限
                $this->view->pvadd = $model->checkPriv($this->_pvprefix . "_ua", false);
                //获取编辑权限
                $this->view->pvedit = $model->checkPriv($this->_pvprefix . "_ue", false);
            }
            $this->view->groupid = $auth['groupid'];
            $this->view->users   = $users;
            $this->display();

        }

    }

    //组权限列表
    public function group()
    {

        $priv = array($this->_pvprefix . "_gp");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth  = $auth;
            $this->view->privs = RPHP::loadConfig("config/priv");

            //获取组权限
            $groups = $model->getGroups();

            if ($groups) {
                foreach ($groups as $r) {
                    if ($r['last_aid'] > 0) {
                        $aid[] = $r['last_aid'];
                    }

                }

                if ($aid) {
                    $this->view->aid = $model->getAdminUsername($aid);
                }

                $this->view->groups = $groups;
                $this->display();
            } else {
                RPHP_RR::nodata();
            }

        }

    }

    //新增组
    public function groupadd()
    {

        $priv = array($this->_pvprefix . "_gp");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth  = $auth;
            $this->view->privs = RPHP::loadConfig("config/priv");
            $this->display();
        }

    }

    //编辑组
    public function groupedit()
    {

        $priv = array($this->_pvprefix . "_gp");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth  = $auth;
            $this->view->privs = RPHP::loadConfig("config/priv");
            //获取组权限
            $group = $model->getGroups(intval($_GET['groupid']));
            if ($group) {
                if ($group['last_aid'] > 0) {
                    $aid[] = $group['last_aid'];
                }

                if ($aid) {
                    $this->view->aid = $model->getAdminUsername($aid);
                }

                $this->view->group = $group;

                $this->display();
            } else {
                RPHP_RR::nodata();
            }

        }

    }

    //保存组
    public function groupdo()
    {

        $code = 900;

        $priv = array($this->_pvprefix . "_gp");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            if ($_POST['act'] == "add") {

                $this->view->success = "新增组成功";
                $this->view->link    = array(
                    array('name' => '继续新增组', 'url' => '/system/groupadd'),
                    array('name' => '返回组权限列表', 'url' => '/system/group'),
                );

                $code = $model->saveGroup($_POST);

            } elseif ($_POST['act'] == "edit" && intval($_POST['groupid']) > 0) {

                $this->view->success = "编辑组权限成功";
                $this->view->link    = array(
                    array('name' => '返回组列表', 'url' => '/system/group'),
                );

                $code = $model->saveGroup($_POST, intval($_POST['groupid']));

            }

            $this->view->code = $code;

            $this->displaydo();

        }

    }

}
